package com.article.service.impl;

import com.article.entity.ApiResult;
import com.article.entity.model.ArticleCategory;
import com.article.entity.model.ArticleCategoryExample;
import com.article.mapper.ArticleCategoryMapper;
import com.article.service.ArticleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    @Override
    public ApiResult addArticleCategory(ArticleCategory param) {
        int affected = articleCategoryMapper.insertSelective(param);
        if (affected > 0){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult deleteArticleCategoryById(ArticleCategory param) {
        int affected = articleCategoryMapper.deleteByPrimaryKey(param.getId());
        if (affected > 0){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult deleteArticleCategoryRecursiveById(ArticleCategory param) {
        List<ArticleCategory> articleCategories = articleCategoryMapper.selectRecursiveByPrimaryKey(param.getId());
        if (articleCategories.size() > 0){
            ArticleCategoryExample articleCategoryExample = new ArticleCategoryExample();
            ArticleCategoryExample.Criteria criteria = articleCategoryExample.createCriteria();
            criteria.andIdIn(articleCategories.stream().map(ArticleCategory::getId).collect(Collectors.toList()));
            int affected = articleCategoryMapper.deleteByExample(articleCategoryExample);
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult updateArticleCategoryById(ArticleCategory param) {
        int affected = articleCategoryMapper.updateByPrimaryKeySelective(param);
        if (affected > 0){
            return ApiResult.ok();
        }else {
            return ApiResult.fail();
        }
    }

    @Override
    public ApiResult selectArticleCategoryRecursiveList(ArticleCategory param) {
        ArticleCategoryExample articleCategoryExample = new ArticleCategoryExample();
        articleCategoryExample.setOrderByClause("create_time desc");
        ArticleCategoryExample.Criteria criteria = articleCategoryExample.createCriteria();
        List<ArticleCategory> articleCategories = articleCategoryMapper.selectByExample(articleCategoryExample);
        List<ArticleCategory> articleCategoryList = buildByRecursive(articleCategories);
        return ApiResult.ok(articleCategoryList);
    }

    // 无限级分类
    public List<ArticleCategory> buildByRecursive(List<ArticleCategory> articleCategories){
        ArrayList<ArticleCategory> trees = new ArrayList<>();
        for (ArticleCategory articleCategory : articleCategories) {
            if(articleCategory.getParentId().equals(0)){
                trees.add(findChildren(articleCategory,articleCategories));
            }
        }
        return trees;
    }

    public ArticleCategory findChildren(ArticleCategory articleCategory,List<ArticleCategory> articleCategories){
        for ( ArticleCategory it : articleCategories ) {
            if (articleCategory.getId().equals(it.getParentId())){
                if (articleCategory.getChildren() == null){
                    articleCategory.setChildren(new ArrayList<ArticleCategory>());
                }
                articleCategory.getChildren().add(findChildren(it,articleCategories));
            }
        }
        return articleCategory;
    }
}
